package cc.orange.utils;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Base64;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import org.xutils.common.util.MD5;

import java.util.List;
import java.util.UUID;

import demo.smart.access.xutlis.util.ZXSharedPrefUtil;
import demo.smart.access.xutlis.util.ZXTimeUtil;

/**
 * Created by CM on 2021/3/1.
 */

public class RequestPermissions {

//   public static void getPermissions_storage(Context context ,SuccessInterf success){
//
////       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//           XXPermissions.with(context)
////                   .permission(Permission.READ_EXTERNAL_STORAGE)
////                   .permission(Permission.WRITE_EXTERNAL_STORAGE)
//                   .permission(Permission.MANAGE_EXTERNAL_STORAGE)
////                        .permission(Permission.Group.STORAGE)// 申请多个权限
//                   .request(new OnPermissionCallback() {
//                       @Override
//                       public void onGranted(List<String> permissions, boolean all) {
//                           if (all) {//全部
//                               success.onSuccess();
//                           } else {//部分
////                               ZXToastUtil.showToast("存储权限获取失败，请在设置中修改");
//                               PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开存储和相机相关权限");
//                           }
//                       }
//
//                       @Override
//                       public void onDenied(List<String> permissions, boolean never) {
//                           if (never) {
////                               ZXToastUtil.showToast("存储权限被拒绝，请在设置中修改");
//                               // 如果是被永久拒绝就跳转到应用权限系统设置页面
////                               XXPermissions.startPermissionActivity(context, permissions);
////                               PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开存储和相机相关权限");
//                           } else {
////                               ZXToastUtil.showToast("获取权限失败");
//                           }
//                           PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开存储和相机相关权限");
//                       }
//                   });
////       }else {
////
////       }
//   }

    public static void getPermissions_camera(Context context ,SuccessInterf success){
//       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        XXPermissions.with(context)
                .permission(Permission.MANAGE_EXTERNAL_STORAGE)
                .permission(Permission.CAMERA)
                .interceptor(new PermissionInterceptor())
//                        .permission(Permission.Group.STORAGE)// 申请多个权限
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {//全部
                            success.onSuccess();
                        } else {//部分
                               ZXToastUtil.showToast("存储权限获取失败，请在设置中修改");
//                           ZX(context,"您需要在设置中打开相机相关权限");
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
//                               ZXToastUtil.showToast("存储权限被拒绝，请在设置中修改");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                               XXPermissions.startPermissionActivity(context, permissions);
//                               PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开存储和相机相关权限");
                        } else {
//                               ZXToastUtil.showToast("获取权限失败");
                        }
//                        PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开相机相关权限");
                    }
                });
//       }else {
//
//       }
    }



    public static void getPermissions_Read(Context context ,SuccessInterf success){
        XXPermissions.with(context)
//                .permission(Permission.READ_CONTACTS)
                .permission(Permission.Group.CONTACTS)
                .interceptor(new PermissionInterceptor())
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {//全部
                            success.onSuccess();
                        } else {//部分
                            ZXToastUtil.showToast("通讯录权限获取失败，请在设置中修改");
//                            PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开通讯录权相关权限");
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
//                            ZXToastUtil.showToast("通讯录权限被拒绝，请在设置中修改");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                            XXPermissions.startPermissionActivity(context, permissions);

//                            PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开通讯录相关权限");
                        } else {
//                            ZXToastUtil.showToast("获取权限失败");
                        }
//                        PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开通讯录权相关权限");
                    }

                });
    }

    public static void getPermissions_location(Context context ,SuccessInterf success){
        XXPermissions.with(context)
//                .permission(Permission.ACCESS_BACKGROUND_LOCATION)
                .permission(Permission.ACCESS_COARSE_LOCATION)
                .permission(Permission.ACCESS_FINE_LOCATION)
                .interceptor(new PermissionInterceptor())
//                .permission(Permission.NOTIFICATION_SERVICE)
//                .permission(Permission.REQUEST_INSTALL_PACKAGES)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {//全部
                            success.onSuccess();
                            ZXSharedPrefUtil zx = new ZXSharedPrefUtil();
                            zx.putString("locations_time", "");
                        } else {//部分
                            ZXSharedPrefUtil zx = new ZXSharedPrefUtil();
                            zx.putString("city_name", "");
                            zx.putString("locations_time", ZXTimeUtil.getCurrentTime());
//                            ZXToastUtil.showToast("定位权限获取失败，请在设置中修改");
//                            success.onDenied(permissions);
//                            PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开定位相关权限");
//                            EventBus.getDefault().post(new LocationDeniedEntity(2));
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            ZXSharedPrefUtil zx = new ZXSharedPrefUtil();
                            zx.putString("city_name", "");
                            zx.putString("locations_time", ZXTimeUtil.getCurrentTime());
                            zx.putString("lat", "");
                            zx.putString("lon","");
//                            ZXToastUtil.showToast("定位权限被拒绝，请在设置中修改");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                            XXPermissions.startPermissionActivity(context, permissions);
                        } else {
                            ZXSharedPrefUtil zx = new ZXSharedPrefUtil();
                            zx.putString("city_name", "");
                            zx.putString("locations_time", ZXTimeUtil.getCurrentTime());
                            zx.putString("lat", "");
                            zx.putString("lon","");
//                            ZXToastUtil.showToast("获取权限失败");
                        }
//                        PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开定位相关权限");
//                        success.onDenied(permissions);
//                        EventBus.getDefault().post(new LocationDeniedEntity(2));
                    }
                });
    }

    public interface SuccessInterf{
        void onSuccess();
//        abstract void onDenied(List<String> permissions);
    }



    public static void getPermissions_storage(Context context ,SuccessInterf success){

//       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        XXPermissions.with(context)
//                   .permission(Permission.READ_EXTERNAL_STORAGE)
//                   .permission(Permission.WRITE_EXTERNAL_STORAGE)
                .permission(Permission.MANAGE_EXTERNAL_STORAGE)
                .interceptor(new PermissionInterceptor())
//                        .permission(Permission.Group.STORAGE)// 申请多个权限
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {//全部
                            success.onSuccess();
                        } else {//部分
                               ZXToastUtil.showToast("存储权限获取失败，请在设置中修改");
//                            PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开存储相关权限");
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
//                               ZXToastUtil.showToast("存储权限被拒绝，请在设置中修改");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                               XXPermissions.startPermissionActivity(context, permissions);
//                               PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开存储和相机相关权限");
                        } else {
//                               ZXToastUtil.showToast("获取权限失败");
                        }
//                        PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开存储相关权限");
                    }
                });
//       }else {
//
//       }
    }


    public static boolean checkPermissions(Context context) {
        PackageManager pm = context.getPackageManager();
        boolean b1 = PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.ACCESS_COARSE_LOCATION", "cc.orange");
        boolean b2 = PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.ACCESS_FINE_LOCATION", "cc.orange");
        return b1 && b2;
    }

    public static void getPermissions_locationTalk(Context context ,SuccessInterf success){
        XXPermissions.with(context)
                .permission(Permission.READ_PHONE_STATE)
                .permission(Permission.ACCESS_COARSE_LOCATION)
                .permission(Permission.ACCESS_FINE_LOCATION)
                .interceptor(new PermissionInterceptor())
//                .permission(Permission.REQUEST_INSTALL_PACKAGES)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {//全部
                            success.onSuccess();
                            ZXSharedPrefUtil zx = new ZXSharedPrefUtil();
                            zx.putString("locations_time", "");
                        } else {//部分
                            ZXSharedPrefUtil zx = new ZXSharedPrefUtil();
                            zx.putString("city_name", "");
                            zx.putString("locations_time", ZXTimeUtil.getCurrentTime());
//                            ZXToastUtil.showToast("定位权限获取失败，请在设置中修改");
//                            success.onDenied(permissions);
//                            PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开定位相关权限");
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            ZXSharedPrefUtil zx = new ZXSharedPrefUtil();
                            zx.putString("city_name", "");
                            zx.putString("locations_time", ZXTimeUtil.getCurrentTime());
                            zx.putString("lat", "");
                            zx.putString("lon","");
//                            ZXToastUtil.showToast("定位权限被拒绝，请在设置中修改");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                            XXPermissions.startPermissionActivity(context, permissions);

                        } else {
                            ZXSharedPrefUtil zx = new ZXSharedPrefUtil();
                            zx.putString("city_name", "");
                            zx.putString("locations_time", ZXTimeUtil.getCurrentTime());
                            zx.putString("lat", "");
                            zx.putString("lon","");
//                            ZXToastUtil.showToast("获取权限失败");
                        }
//                        PermissionCheckUtil.showRequestPermissionLocationsFailedAlter(context,"您需要在设置中打开定位相关权限");
//                        success.onDenied(permissions);
                    }
                });
    }

    public static void getPermissions_notfi(Context context ,SuccessInterf success) {
        XXPermissions.with(context)
                .permission(Permission.NOTIFICATION_SERVICE)
                .interceptor(new PermissionInterceptor())
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {//全部
                            success.onSuccess();
                        } else {//部分
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                        } else {
                        }
                    }
                });

    }


    //获得独一无二的Psuedo ID
    public static String getUniquePsuedoID() {
        String serial = null;
        String m_szDevIDShort = "35" +  Build.BOARD.length()%10+ Build.BRAND.length()%10 +
                Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 +
                Build.DISPLAY.length()%10 + Build.HOST.length()%10 +
                Build.ID.length()%10 + Build.MANUFACTURER.length()%10 +
                Build.MODEL.length()%10 + Build.PRODUCT.length()%10 +
                Build.TAGS.length()%10 + Build.TYPE.length()%10 +
                Build.USER.length()%10 ; //13 位
        try {
            serial = android.os.Build.class.getField("SERIAL").get(null).toString();
            //API>=9 使用serial号

            String uniqueP=  new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
            String s = MD5.md5(uniqueP);
            return Base64.encodeToString(s.getBytes(),Base64.DEFAULT);
        } catch (Exception exception) {
            //serial需要一个初始化
            serial = "laires"; // 随便一个初始化
        }
        // 使用硬件信息拼凑出来的15位号码
        // 最终会得到这样的一串ID：00000000-28ee-3eab-ffff-ffffe9374e72
        String uniqueP=  new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
        String s = MD5.md5(uniqueP);
        return Base64.encodeToString(s.getBytes(),Base64.DEFAULT);
    }
}
